#pragma once
#include <mythread.hxx>
#include <safeque.hxx>
#include <vector>
#include <map>

class FileLog;
class Robot;
class Task;
class TaskManager;
class RobotManager : public Thread
{
public:
	RobotManager(TaskManager* taskManager, int PoolSize = 10, int MaxPoolSize = 200, FileLog* filelog = NULL)
		: taskManager(taskManager),
		robots(),
		robotsInUse(),
		PoolSize(PoolSize),
		MaxPoolSize(MaxPoolSize),
		isShutdownRequested(false),
		_filelog(filelog)
	{
		Start();
	}

	void Shutdown(void);
	virtual void Execute(Thread::Arg arg);

private:
	friend class Robot;
	Task* GetReadyTask(void);
	
private:
	TaskManager* taskManager;
	std::vector<Robot*> robots;
	mutable Mutex robotsInUse;
	const int PoolSize;
	const int MaxPoolSize;
	bool isShutdownRequested;
	FileLog* _filelog;

	int SpawnRobots(int num = 1, bool isStanding = true);
	void CleanFinishedRobots(void);
};